#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<signal.h>
#include"../include/client.h"


#define INTERVAL 10//采样周期
#define PACK_SIZE 200

void sig_quit(int signal);

void sig_timer(int signal);

resinfo server_ack;//服务器响应

int get_stop = 0;//结束标志
int sampleover_statu = 0;//采样标志

float temp;//温度缓存区
char chip[20];//设备编号缓存区

int main(int argc,char **argv)
{
	char                   		*serv_IP = NULL;
	int                    		serv_port = 0;
	char                        allbuff[100];//整合设备号、温度、时间
	char                        mytime[50];//缓存时间
	unsigned char			    pack_buff[PACK_SIZE];//待发送数据包缓存区
	int				            pack_size = 0;//数据包大小
	int                    		sockfd = -1;
	int                    		rv = -1;
	
    rv = argument_parse(argc,argv,&serv_IP,&serv_port);//参数解析
    if(rv < 0)
    {
        return 0;//退出程序
    }
    else
    {
        printf("解析出服务器serv_IP为:%s\n",serv_IP);
        mylog(__func__,__LINE__,INFO,"解析出服务器IP为:%s",serv_IP);

        printf("解析出服务器端口为:%d\n",serv_port);
        mylog(__func__,__LINE__,INFO,"解析出服务器端口为:%d",serv_port);
    }

    //初始化缓冲区
    memset(allbuff,0,sizeof(allbuff));
    memset(mytime,0,sizeof(mytime));
    
    //ctrl+c退出程序
	if(signal(SIGQUIT,sig_quit) < 0)
	{
		printf("signal SIGKILL failed:%s\n",strerror(errno));
        mylog(__func__,__LINE__,ERROR,"signal SIGQUIT faild:%s",strerror(errno));
        return 0;

    }

	//安装定时采样信号
	if(signal(SIGALRM,sig_timer) < 0)
	{
		printf("signal SIGALRM faild:%s\n",strerror(errno));
        mylog(__func__,__LINE__,ERROR,"signal SIGALRM faild:%s",strerror(errno));
        return 0;
	}

	//网络套接字初始化
   sockfd = socket_init(serv_IP,serv_port);
   if(sockfd < 0)
   {
        return 0;//退出程序
   }

   //初始化服务器响应结构体
   server_ack.state = 1;

	alarm(INTERVAL);//定时采样

	while(!get_stop)
	{
		if(sampleover_statu == 1)//采样结束，开始上传至服务器
		{

            //清空缓冲区
            memset(allbuff,0,sizeof(allbuff));
            memset(mytime,0,sizeof(mytime));
            memset(pack_buff,0,sizeof(pack_buff));//清空数据包
			
            rv = get_date_time(mytime);//获取日期和时间
            if(rv < 0)
            {
                sampleover_statu = 0;//采样恢复,重新采样
                printf("Don't get time , recover sample!\n");
                mylog(__func__,__LINE__,WARN,"Don't get time , recover sample!");
                continue;
            }

		    sprintf(allbuff,"%s %s %.3f",mytime,chip,temp);//整合时间+设备号+温度

            pack_size = tlv_pack(allbuff, strlen(allbuff), pack_buff);//tlv打包数据
            if(pack_size < 0)
            {
                sampleover_statu = 0;//采样恢复,重新采样
                printf("Pack size is 0, recover sample!\n");
                mylog(__func__,__LINE__,WARN,"Pack size is 0, recover sample!");
                continue;
            }
		
            //发送数据到服务器
	        rv = send_pack_to_server(sockfd,pack_buff,pack_size+1);
            if(rv < 0)
            {
                sampleover_statu = 0;//采样恢复,重新采样
                printf("pack_size is 0,send failed,recover sample sate!\n");
                mylog(__func__,__LINE__,WARN,"pack_size is 0,send failed,recover sample sate!");
                continue;
            }
            
			//等待服务器响应
            server_ack.state = 0;//默认没有响应
            memset(server_ack.resbuff,0,sizeof(server_ack.resbuff));
            rv =  wait_response(sockfd,&server_ack);
            if(rv < 0)
            {
                goto cleanup;
            }

			
           //释放内存 
            printf("%s\n",server_ack.resbuff);
            mylog(__func__,__LINE__,INFO,"%s",server_ack.resbuff);

			sampleover_statu = 0;//采样恢复
          
		}

	}

	close(sockfd);

cleanup:
	close(sockfd);
	return 0;

}


void sig_quit(int signo)
{
	get_stop = 1;
    printf("ctrl+c Program exited!\n");
    mylog(__func__,__LINE__,INFO,"ctrl+c Program exited!");
}

void sig_timer(int signo)
{
    if(server_ack.state)
    {
        printf("client is starting sample...\n");
        mylog(__func__,__LINE__,INFO,"client is starting sample...");
        //DS_get_tem(&temp,chip);
        temp = 15.063;    
        strcpy(chip,"28-041731f7c0ff");
        sampleover_statu = 1;
        alarm(INTERVAL);  
    }
	else
    {
        printf("server reponsed timeout! Program will be exited!\n");
        mylog(__func__,__LINE__,INFO,"server reponsed timeout! Program will be exited!");
        exit(0);

    }
}





